#include "admin_edit.h"
#include "ui_admin_edit.h"

admin_edit::admin_edit(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::admin_edit)
{
    ui->setupUi(this);
}

admin_edit::~admin_edit()
{
    delete ui;
}
//返回管理员界面
void admin_edit::on_pushButton_clicked()
{
    this->close();
}
//增加管理员or用户
void admin_edit::on_pushButton_8_clicked()
{
    QApplication::setQuitOnLastWindowClosed(false);// 防止弹窗直接关闭程序
    ui->password1->setEchoMode(QLineEdit::Password);;//密文输入
    ui->password2->setEchoMode(QLineEdit::Password);;//密文
    QString name = ui->name->text();
    QString phone=ui->phone->text();
    QString email=ui->email->text();
    QString username=ui->username->text();
    QString password = ui->password1->text();
    QString password2 = ui->password2->text();
    QDateTime Datetime=QDateTime::currentDateTime();
    QString time=Datetime.toString("yyyy-MM-dd hh:mm:ss");//当前时间字符串格式
    QString carid=ui->carid->text();//车牌号
    QString cartype=ui->carid->text();//汽车类型
    QString flag;
    //未输入用户类型
    if(!ui->radioButton->isChecked()&&!ui->radioButton_2->isChecked()){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("请选择用户类型"), QMessageBox::Yes);
    }
    else if(name.isEmpty() || phone.isEmpty() || email.isEmpty() || username.isEmpty()
            || password.isEmpty() || password2.isEmpty() ||carid.isEmpty()){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("输入的信息不完整"), QMessageBox::Yes);
    }
    else if(password != password2){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("两次输入的密码不相同"), QMessageBox::Yes);
    }
    else{
        if(ui->radioButton->isChecked()){
            flag="管理员";//将标签设置为普通用户
        }//选了管理员
        else if(ui->radioButton_2->isChecked()){
            flag="普通用户";
        }//选了用户
        QSqlQuery query;
        QString str = QString("insert into userinfo(id,name,flag,carid,phone,email,username,password,time,fare) values(0,'%1', '%2', '%3', '%4','%5', '%6','%7','%8',0);").arg(name).arg(flag).arg(carid).arg(phone)
                .arg(email).arg(username).arg(password).arg(time);//0代表id自增
        //注册汽车信息
        QString str_2 = QString("insert into carinfo(id,carid,cartype,username) values(0, '%1', '%2','%3')").arg(carid).arg(cartype).arg(username);
        //qDebug()<<query.lastError().text();//显示数据库错误
        if(query.exec(str)&& query.exec(str_2)){            //
                QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("增加成功！"), QMessageBox::Yes);
                this->close();//增加页面关闭
        }
        else{
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("增加失败！"), QMessageBox::Yes);
        }


    }

}

//修改信息
void admin_edit::on_pushButton_4_clicked()
{
    //获取要修改的账号
    QString username =ui->lineEdit_4->text();
    QString name =ui->name_4->text();
    QString phone =ui->phone_4->text();
    QString email =ui->email_4->text();
    if(username.isEmpty() || name.isEmpty() || phone.isEmpty() || email.isEmpty()){
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("输入的信息不完整"), QMessageBox::Yes);
        }
    else{
        QSqlQuery query;
        QString str = QString("update userinfo set name='%1',phone='%2',email='%3' where username='%4';").arg(name).arg(phone).arg(email).arg(username);
        if(query.exec(str)){
                QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("修改成功！"), QMessageBox::Yes);
                this->close();//页面关闭
                emit(send_currentinfo_flush());//更新管理员账号信息
        }
        else{
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("修改失败！"), QMessageBox::Yes);
        }
    }


}
//删除信息
void admin_edit::on_pushButton_6_clicked()
{
    QString username =ui->lineEdit_5->text();
    if(username.isEmpty()){
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("输入的信息不完整"), QMessageBox::Yes);
        }
    else{
        QSqlQuery query;
        QString str = QString("delete from userinfo where username='%1';").arg(username);
        if(query.exec(str)){
                QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("删除成功！"), QMessageBox::Yes);
                this->close();//页面关闭
                emit(send_currentinfo_flush());//更新管理员账号信息
        }
        else{
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("删除失败！"), QMessageBox::Yes);
        }
    }
}
